package com.itboye.textileexpres.shopcart;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.tbshopcar.listener.OnShoppingCartChangeListener;
import com.itboye.tbshopcar.listener.ShoppingCartBiz;
import com.itboye.textileexpes.beans.CopyShopCartModel;
import com.itboye.textileexpes.beans.CreateOrderBeans;
import com.itboye.textileexpes.beans.MyOrderBeans;
import com.itboye.textileexpes.beans.ShopCartModel;
import com.itboye.textileexpes.beans.YoufeiBean;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpes.presenter.YouFeiPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.alipaytest.ActivityPaymentMethod;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.myset.ActivityAddressManage;
import com.itboye.textileexpres.shopcart.ShopcartExpandableListViewAdapter.CheckInterface;
import com.itboye.textileexpres.shopcart.ShopcartExpandableListViewAdapter.ModifyCountInterface;
import com.itboye.textileexpres.top.ActivityBuyNow;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.PriceChange;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class ActivityShopCarOrder extends BaseActivity implements Observer {
	TextView title;
	ImageView back;
	ImageView img_other;
	CopyShopCartModel cartModel = new CopyShopCartModel();
	boolean isAllEditing = false;
	ExpandableListView orderlist;
	LinearLayout container_lin;
	String price;
	String addressID;
	String name;
	String detailInfo;
	String phone;
	String pids;
	TextView tvprice;
	TextView choose_tv;
	LinearLayout address_lin;
	LinearLayout choose_address_lin;
	TextView shouhuo_name, textPhone, address_tv;
	List<YoufeiBean> youfeibean = new ArrayList<>();
	YouFeiPresenter youFeiPresenter;
	TextView queren_tv;
	TextView total_price;
	TextView heji_price;
	StringBuffer shopcarBuffer = new StringBuffer();
	CreateOrderBeans myOrderBeans;
	MyOrderPresenter myOrderPresenter;
	EditText note;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_shopcar_order;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText("确认订单");
		queren_tv.setEnabled(false);
		youFeiPresenter = new YouFeiPresenter(this);
		myOrderPresenter = new MyOrderPresenter(this);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		choose_tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityShopCarOrder.this,
						ActivityAddressManage.class);
				intent.putExtra("type", "goods_detail");
				startActivityForResult(intent, 101);
			}
		});
		cartModel = (CopyShopCartModel) getIntent().getSerializableExtra(
				"selected");
		price = getIntent().getStringExtra("totalPrice");
		total_price.setText(price);
		for (int i = 0; i < cartModel.getInfo().size(); i++) {
			View groupview = LayoutInflater.from(ActivityShopCarOrder.this)
					.inflate(R.layout.shopcart_group, null);
			LinearLayout child_view_lin = (LinearLayout) groupview
					.findViewById(R.id.child_view_lin);
			TextView publish_name = (TextView) groupview
					.findViewById(R.id.publish_name);
			TextView publish_area = (TextView) groupview
					.findViewById(R.id.publish_area);
			publish_area.setText(cartModel.getInfo().get(i).getPlace_origin());
			publish_name
					.setText(cartModel.getInfo().get(i).getPublisher_name());
			for (int j = 0; j < cartModel.getInfo().get(i).getItems().size(); j++) {
				if (cartModel.getInfo().get(i).getItems().get(j)
						.isChildSelected()) {
					shopcarBuffer.append(","
							+ cartModel.getInfo().get(i).getItems().get(j)
									.getId());
					View view = LayoutInflater.from(ActivityShopCarOrder.this)
							.inflate(R.layout.shaopcar_childview, null);
					ImageView check_box_img = (ImageView) view
							.findViewById(R.id.check_box_img);
					ImageView goods_img = (ImageView) view
							.findViewById(R.id.goods_img);
					TextView goods_name1_tv = (TextView) view
							.findViewById(R.id.goods_name1_tv);
					TextView goods_detail_tv = (TextView) view
							.findViewById(R.id.goods_detail_tv);
					TextView color_tv = (TextView) view
							.findViewById(R.id.color_tv);
					TextView price_tv = (TextView) view
							.findViewById(R.id.price_tv);
					TextView price_unit_tv = (TextView) view
							.findViewById(R.id.price_unit_tv);
					TextView num_tv = (TextView) view.findViewById(R.id.num_tv);
					LinearLayout type_1 = (LinearLayout) view
							.findViewById(R.id.type_1);
					LinearLayout type_3 = (LinearLayout) view
							.findViewById(R.id.type_3);
					RelativeLayout type_2 = (RelativeLayout) view
							.findViewById(R.id.type_2);
					type_2.setVisibility(View.GONE);
					type_1.setVisibility(View.VISIBLE);
					type_3.setVisibility(View.GONE);
					check_box_img.setVisibility(View.GONE);
					XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
							+ cartModel.getInfo().get(i).getItems().get(j)
									.getIcon_url(), goods_img);
					goods_name1_tv.setText(cartModel.getInfo().get(i)
							.getItems().get(j).getProduct_code());
					goods_detail_tv.setText(cartModel.getInfo().get(i)
							.getItems().get(j).getName());
					color_tv.setText(cartModel.getInfo().get(i).getItems()
							.get(j).getSku_desc());
					price_tv.setText(PriceChange.Change(cartModel.getInfo()
							.get(i).getItems().get(j).getPrice()));
					price_unit_tv.setText("/"
							+ cartModel.getInfo().get(i).getItems().get(j)
									.getUnit_desc());
					num_tv.setText("X"
							+ cartModel.getInfo().get(i).getItems().get(j)
									.getCount());
					child_view_lin.setVisibility(View.VISIBLE);
					child_view_lin.addView(view);
				}

			}

			container_lin.addView(groupview);
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == YouFeiPresenter.shopcaryoufei_success) {
				youfeibean.addAll((List<YoufeiBean>) entity.getData());
				queren_tv.setEnabled(true);
				tvprice.setText(PriceChange.Change(youfeibean.get(0)
						.getFreight()));
				heji_price.setText(""
						+ (Double.parseDouble(price) + Double
								.parseDouble(tvprice.getText() + "")));
				queren_tv.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						myOrderBeans = new CreateOrderBeans();
						myOrderBeans.setAddress_id(addressID);
						myOrderBeans.setDel("0");
						myOrderBeans.setUid(IsMobileNumPhone.isUid());
						myOrderBeans.setIds(shopcarBuffer.substring(1,
								shopcarBuffer.length()));
						myOrderBeans.setNote(note.getText() + "");
						myOrderPresenter.createOrder(myOrderBeans);
					}
				});
			} else if (entity.getEventType() == YouFeiPresenter.shopcaryoufei_fail) {
				heji_price.setText("" + (Double.parseDouble(price)));
			}
			if (entity.getEventType() == MyOrderPresenter.Create_success) {
				myOrderBeans = (CreateOrderBeans) entity.getData();
				String price = myOrderBeans.getPay_money();
				String payCode = myOrderBeans.getPay_code();
				String paySign = myOrderBeans.getSign();
				Intent intent = new Intent(ActivityShopCarOrder.this,
						ActivityPaymentMethod.class);
				intent.putExtra("price", PriceChange.Change(price));
				intent.putExtra("orderCode", payCode + "");
				startActivity(intent);
			} else if (entity.getEventType() == MyOrderPresenter.Create_fail) {
				toast(entity.getData() + "");
			}
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 101 && arg1 == 102) {
			if (arg2 != null) {
				address_lin.setVisibility(View.VISIBLE);
				choose_address_lin.setVisibility(View.GONE);
				addressID = arg2.getStringExtra("id");
				name = arg2.getStringExtra("name");
				phone = arg2.getStringExtra("phone");
				detailInfo = arg2.getStringExtra("address");
				shouhuo_name.setText(name);
				textPhone.setText(phone);
				address_tv.setText(detailInfo);

				youFeiPresenter.shopcarYouFei(IsMobileNumPhone.isUid(),
						shopcarBuffer.substring(1, shopcarBuffer.length()),
						addressID);

			}

		}
	}

}
